class OrdersController < ApplicationController
  # GET /orders/new
  # GET /orders/new.xml
  def new
    @cart = current_cart
    @order = Order.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @order }
    end
  end

  # POST /orders
  # POST /orders.xml
  def create
    @order = Order.new(params[:order])
    @order.client_id = current_user.client.id
    @order.total_amount = current_cart.total_amount
    @order.current_cart = current_cart

    respond_to do |format|
      if @order.valid? && @order.save
        current_cart.mark_as_ordered
        SavedCart.empty_cart(current_user.client.id)

        format.html { redirect_to(show_order_success_message_url) }
      else
        format.html { render :action => "new" }
      end
    end
  end

  def success_message
    respond_to do |format|
      format.html
    end
  end
end
